<?
require 'global.php';
$url='wallet';

if($customer_id==0) { header('Location:login.html'); exit; }
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"]; // 当前页码 current page
$sort=1; if(isset($_GET["sort"]))if(is_numeric($_GET["sort"]))$sort=$_GET["sort"];
$type=1; if(isset($_GET["type"]))if(is_numeric($_GET["type"]))$type=$_GET["type"];
$gee=$customer;
// 错误集 error array
$errors=array();

switch ($sort) {
    case 1:
        $order_by='id';
        $order_direction='desc';
        break;
    case 2:
        $order_by='price';
        $order_direction='asc';
        break;
    case 3:
        $order_by='price';
        $order_direction='desc';
        break;
}
$recode->rows=6;
$customer_info=$customer->get_row($customer_id);
$where = array();
$where[] = array('name'=>'type', 'val'=>$type, 'opn'=>'=', 'and'=>'and');
$where[] = array('name'=>'customer_id', 'val'=>$customer_id, 'opn'=>'=', 'and'=>'and');
if($type==1){
    $where[] = array('name'=>'template_id', 'val'=>2, 'opn'=>'=', 'and'=>'and');
}elseif($type == 2){
    $where[] = array('name'=>'template_id', 'val'=>3, 'opn'=>'=', 'and'=>'and');
}
$pages=$recode->count_pages($where);
$info=$recode->get_page($page,$where,$order_by,$order_direction);
$wallet_info = array();
if($type==1){
    $template_i1 = $template->get_row(1);
    $template_i2 = $template->get_row(2);
    foreach($info as $key=>$val){
        $where_t = array();
        $where_t[] = array('name'=>'type', 'val'=>1, 'opn'=>'=', 'and'=>'and');
        $where_t[] = array('name'=>'template_id', 'val'=>1, 'opn'=>'=', 'and'=>'and');
        $where_t[] = array('name'=>'customer_id', 'val'=>$customer_id, 'opn'=>'=', 'and'=>'and');
        $where_t[] = array('name'=>'recode_name', 'val'=>$val['recode_name'], 'opn'=>'=', 'and'=>'and');
        $where_t[] = array('name'=>'recode_value', 'val'=>$val['recode_value'], 'opn'=>'=', 'and'=>'and');
        $wallet_type1 = $recode->get_rows_advanced(1, $where_t);
        $recode_info = explode('|', $val['value']);
        $wallet_info[] = array(
            'type_name' => $code_array['recode_type'][1]['name_'.$lang],
            'recode_name' => $val['recode_name'],
            'recode_value' => $val['recode_value'],
            'create_time' => $val['create_time'],
            'amount' => $recode_info[3],
            'refund' => $recode_info[1],
            'temp1' => array(
                'time' => $wallet_type1[0]['create_time'],
                'amount' => 0,
                'description' => str2replace($wallet_type1[0]['value'], $template_i1['content'])
            ),
            'temp2' => array(
                'time' => $val['create_time'],
                'amount' => $recode_info[3],
                'description' => str2replace($val['value'], $template_i2['content'])
            )
        );
    }
}elseif($type == 2){
    $template_i3 = $template->get_row(3);
    foreach($info as $key=>$val){
        $recode_info = explode('|', $val['value']);
        $wallet_info[] = array(
            'type_name' => $code_array['recode_type'][2]['name_'.$lang],
            'recode_name' => $val['recode_name'],
            'recode_value' => $val['recode_value'],
            'create_time' => $val['create_time'],
            'amount' => $recode_info[3],
            'description' => str2replace($val['value'], $template_i3['content'])
        );
    }

}
//pre($wallet_info);

// SEO
$cate_name=WEB_WALLET_MY;
$title=$cate_name.' - '.$config['SITE_COMPANY_'.LANG];
$dpn=$kwd=$title;


function str2replace($str, $value){
    $arr = explode("|", $str);
    foreach($arr as $val){
        $value = preg_replace('/%s/', $val, $value, 1);
    }
    return $value;
}
// view 加载视图
require VIEW.$url.'.php';

// end of file
